package com.cby.leetcode.二叉树.LC112;

import com.cby.leetcode.二叉树.LC617.LC617;

public class LC112 {

    /**
     * 求是否存在根到叶的路径和 = target
     * 即求递归左右子树是否存在根到叶的路径和 = target - 当前父节点值
     * 递归解题
     *
     * @param args
     */


    public static void main(String[] args){

    }

    static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(){}

        TreeNode(int val, TreeNode left, TreeNode right){
            this.val = val;
            this.left = left;
            this.right = right;
        }


    }

    static class Solution{

        public static boolean hasPathSum(TreeNode root, int targetSum) {
            //根节点为null，直接判false
            if(root == null)
                return false;

            //递归至根节点，判断值
            if(root.left == null && root.right == null){
                //根节点
                if(targetSum == root.val)
                    return true;
                return false;
            }

            //分别递归左右子树是否存在根到叶的路径和 = target
            boolean leftb = hasPathSum(root.left, targetSum - root.val);
            boolean rightb = hasPathSum(root.right, targetSum - root.val);

            return leftb || rightb;
        }

    }

}
